#!/usr/bin/env python
# # -*- coding: utf-8 -*-

"""
@File:      doc2docx_osx.py
@Author:    Jim.Dai.Cn
@Date:      2024/6/18 22:13
@Desc:         
"""

# have libre-office installed as /Applications/LibreOffice.app
# wget https://mirrors.nju.edu.cn/tdf/libreoffice/stable/7.6.7/mac/x86_64/LibreOffice_7.6.7_MacOS_x86-64.dmg

import subprocess
import os
import warnings


def doc_to_docx_libre(doc_path, docx_path):
    command = ["/Applications/LibreOffice.app/Contents/MacOS/soffice", "--headless", "--convert-to", "docx", "--outdir", os.path.dirname(docx_path), doc_path]
    try:
        # with open(subprocess.DEVNULL, 'w') as devnull:
        subprocess.run(command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True)
        # subprocess.call(command)
        print(f"  == convert to => {docx_path}")
    except subprocess.CalledProcessError as e:
        print(f"  == 转换过程中出现错误: {e}")

# 使用示例
# doc_file = "/Users/jim/development/workspace/hzw/BidAI/BidScanner/round1_test_data/中华人民共和国企业国有资产法.doc"
# docx_file = "/Users/jim/development/workspace/hzw/BidAI/BidScanner/tmp/中华人民共和国企业国有资产法.docx"
# doc_to_docx_libre(doc_file, docx_file)